if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
if (!localStorage.arr_danwei) {
    localStorage.arr_danwei = JSON.stringify([]);
}
if (!localStorage.arr_chicun) {
    localStorage.arr_chicun = JSON.stringify([]);
}
if (!localStorage.arr_color) {
    localStorage.arr_color = JSON.stringify([]);
}
$(function () {
    // 构造地图
    var add_map = new AMap.Map('add_map_wrap', {
        resizeEnable: true,
        zoom:11,
        center: [116.397428, 39.90923]
    });
    var set_map=new AMap.Map('set_map_wrap', {
        resizeEnable: true,
        zoom:11,
        center: [116.397428, 39.90923]
    });
    // 加载文字提示及选点事件
    AMapUI.loadUI(['misc/PoiPicker'], function(PoiPicker) {
        var poiPicker = new PoiPicker({
            input: 'add_address'
        });
        var setpoiPicker = new PoiPicker({
            input: 'set_address'
        });

        poiPicker.on('poiPicked', function(poiResult) {
            console.log(poiResult);
            $("#add_address").val(poiResult.item.name);
            add_map.setZoomAndCenter(18, [poiResult.item.location.lng, poiResult.item.location.lat]);
        });
        setpoiPicker.on('poiPicked', function(poiResult) {
            console.log(poiResult);
            $("#set_address").val(poiResult.item.name);
            set_map.setZoomAndCenter(18, [poiResult.item.location.lng, poiResult.item.location.lat]);
        });
    });
    // 构造逆地理解析类返回查询函数
    function getgeocoder(){
        var geocoder = new AMap.Geocoder({
            extensions: "all"
        });
        return function (lnglatXY,callBack_) {
            geocoder.getAddress(lnglatXY, function(status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    callBack_(result.regeocode.formattedAddress);
                }
            });
        }
    }
    var add_geocoder=getgeocoder();
    var set_geocoder=getgeocoder();
    var isClickMarker=null;
    var setisClickMarker=null;
    // 地图绑定事件
    add_map.on('click',function(e) {
        console.log(e.lnglat);
        $("#select_point_lat").text(Math.float_(e.lnglat.lat,6));
        $("#select_point_lng").text(Math.float_(e.lnglat.lng,6));
        add_geocoder([Math.float_(e.lnglat.lng,6),Math.float_(e.lnglat.lat,6)],function (address) {
            $("#select_address").text(address);
        });
        if(isClickMarker){
            isClickMarker.setPosition(e.lnglat);
        }else{
            isClickMarker=  new AMap.Marker({
                position: e.lnglat,
                map: add_map,
                animation:'AMAP_ANIMATION_BOUNCE'
            });//绑定
        }

    });
    set_map.on('click',function(e) {
        console.log(e.lnglat);
        $("#set_select_point_lat").text(Math.float_(e.lnglat.lat,6));
        $("#set_select_point_lng").text(Math.float_(e.lnglat.lng,6));
        set_geocoder([Math.float_(e.lnglat.lng,6),Math.float_(e.lnglat.lat,6)],function (address) {
            $("#set_select_address").text(address);
        });
        if(setisClickMarker){
            setisClickMarker.setPosition(e.lnglat);
        }else{
            setisClickMarker=  new AMap.Marker({
                position: e.lnglat,
                map: set_map,
                animation:'AMAP_ANIMATION_BOUNCE'
            });//绑定
        }

    });

    var houtai = {
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        linshi_search: {
            state: null,
            search: "",
            isCheck:"",
            isValidity:""
        },
        linshi_id: null,
        linshi_img_id: null,
        linshi_arrid: [],
        queren_type: null,//0删除 1上架 2下架 3已核实 4未核实 5有效 6无效
        list_ajax_page:1,
        // 导出excel
        excel:(function () {
            var uri = 'data:application/vnd.ms-excel;base64,',
                template = '<html  xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
                base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))); },
                format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); };
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById("tableExcel");
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
                if($("#dlink").length<1) $("body").append($("<a id='dlink' style='display: none'></a>"));
                document.getElementById("dlink").href = uri + base64(format(template, ctx));
                document.getElementById("dlink").download = name+'.xls';
                document.getElementById("dlink").click();
            };
        })(),
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        jiancha: function () {
            // 判断是够为空
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint("请填写 " + $(".add_goods_body .input_is:visible").eq(i).prev().text());
                    return false;
                }
            }
            return true;
        },
        pinpai_fuzhi: function (n) {// n 传过来的三级分类id
            $.ajax({
                url: ip + "index.php?s=admincms/product/specsList",
                type: "post",
                data:{
                    token:get_cookie("key"),
                    catId:n
                },
                success: function (_res) {
                    //写入操作分类table
                    var res=JSON.parse(_res);
                    console.log(res);
                    if(res.errno===0){
                        var string_brand="";
                        if (res.data.brand.length > 0) {
                            $.each(res.data.brand, function (i, o) {
                                string_brand += '<li><a >' + o.names + '</a><p style="display:none;">' + o.bid + '</p></li>';
                            });
                        }
                        $("#down_menu").empty().append(string_brand);
                        var string_specs="";
                        if (res.data.specs.length > 0) {
                            $.each(res.data.specs, function (i, o) {
                                string_specs += '<li><a >' + o.unit + '</a> <p style="display:none;">' + o.sid + '</p></li>';
                            });
                        }
                        $("#down_menu_guige").empty().append(string_specs);
                        var string_color="";
                        if (res.data.color.length > 0) {
                            $.each(res.data.color, function (i, o) {
                                string_color += '<li><a >' + o.names + '</a> <p style="display:none;">' + o.id + '</p></li>';
                            });
                        }
                        $("#down_menu_color").empty().append(string_color);
                    }
                },
                error:function () {
                    houtai.hint("失败！！");
                }

            });

        },
        chushi_class: function () {
            $.ajax({
                url: JdIp + "admin/cats",
                type: "post",
                data: {
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        var data= res.data;
                        console.log(data);
                        $("#add_yewu").empty().append(tpl.compile(''+
                           '<p>选择业务 <span></span>已选择</p>' +
                           '{{for(var i=0;i<$data.length;i++){}}'+
                           '<div class="item_yewu">'+
                                '<b>{{=$data[i].names}}</b>'+
                                '<ul>'+
                                '<li class="all_select">全部</li>'+
                                '{{var $class2=$data[i].class; for(var a=0;a<$class2.length;a++){}}'+
                                   '<li class="item_select" data-id="{{=$class2[a].cid}}">{{=$class2[a].classnames}}</li>'+
                                '{{} }}'+
                               '</ul>'+
                           '</div>'+
                           '{{} }}'
                        )(data));
                        $("#set_yewu").empty().append(tpl.compile(''+
                           '<p>选择业务 <span></span>已选择</p>' +
                           '{{for(var i=0;i<$data.length;i++){}}'+
                           '<div class="item_yewu">'+
                                '<b>{{=$data[i].names}}</b>'+
                                '<ul>'+
                                '<li class="all_select">全部</li>'+
                                '{{var $class2=$data[i].class; for(var a=0;a<$class2.length;a++){}}'+
                                   '<li class="item_select" data-id="{{=$class2[a].cid}}">{{=$class2[a].classnames}}</li>'+
                                '{{} }}'+
                               '</ul>'+
                           '</div>'+
                           '{{} }}'
                        )(data));
                    }
                },
                error: function () {
                    houtai.hint("初始类型失败了！！");
                }
            });
        },
        chushi_danwei:function () {
            var arr = getarr(localStorage.arr_danwei);
            var string_danwei="";
            var string_chicun="";
            $.each(arr, function (i, o) {
                string_danwei += '<li><a >' + o + '</a> <b title="删除" class="glyphicon glyphicon-minus"></b></li>';
            });
            $("#down_menu_danwei").empty().append(string_danwei);
            var arr_chicun = getarr(localStorage.arr_chicun);
            $.each(arr_chicun, function (i, o) {
                string_chicun += '<li><a >' + o + '</a> <b title="删除" class="glyphicon glyphicon-minus"></b></li>';
            });
            $("#down_menu_chicun").empty().append(string_chicun);
        },
        shop_list_ajax: function (page_number) {
            var n = page_number===undefined ? houtai.list_ajax_page=1 : page_number;
            $.ajax({
                url: JdIp + "admin/shops",
                type: "post",
                data: {
                    search:houtai.linshi_search.search,
                    flag:houtai.linshi_search.state,
                    page: n,
                    isValidity:houtai.linshi_search.isValidity,
                    isCheck:houtai.linshi_search.isCheck,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                        houtai.table_string(res);
                        $("#page").sdpagination({
                            boxClass: 'sdpagination',
                            totalCount: res.totalpage,           //总条数
                            pageSize: 15,                //每页显示
                            pageIndex: n,                //当前页数
                            preText: '上页',                                  //上一页按钮显示的字符
                            nextText: '下页',                                 //下一页按钮显示的字符
                            firstText: '首页',                                //首页按钮显示的字符
                            lastText: '末页',                                 //末页按钮显示的字符
                            showNearby: 3,                                    //当前按钮前后按钮数
                            infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                            pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                            onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                                houtai.list_ajax_page=pageIndex;
                                houtai.shop_list_ajax(pageIndex);
                            }
                        });


                }
            });
        },
        table_string: function (res) {
            // 写入table
            if (res.resStatus === 1) {
                var data = res.data;
                $("#shangpin_table").empty().append(tpl.compile('' +
                    '<thead><tr><th>编号</th><th>商家名称</th><th>商家电话</th><th>商家地址</th><th>商家坐标</th><th>经营范围</th><th>图片</th><th>操作</th></tr></thead>' +
                    '<tbody>' +
                        '{{for(var i=0;i<$data.length;i++){}}' +
                        '<tr>' +
                            '<td class="shangjia_id">{{=$data[i].id}}</td>' +
                            '<td class="shangjia_name">{{=$data[i].name}}</td>' +
                            '<td class="shangjia_phone">{{=$data[i].phone}}</td>' +
                            '<td class="shangjia_address">{{=$data[i].addr}}</td>' +
                            '<td class=""><b class="lng" style="margin-right: 10px;">{{=$data[i].longitude}}</b><b class="lat">{{=$data[i].latitude}}</b></td>' +
                            '<td class="click_class2"><b class="btn btn-default btn-xs">点击查看</b></td>' +
                            '<td class="shangjia_banner" style="width: 100px;"><img style="width: 100%;" src="{{=$data[i].banner}}" alt=""></td>' +
                            '<td class="shangjia_control" style="width: 150px">' +
                                '<button class="btn btn-danger btn-xs set">修改</button> ' +
                                '{{if($data[i].flag===0){}}'+
                                  '<button class="btn btn-success btn-xs up_line">上线</button>' +
                                '{{}else{}}'+
                                  '<button class="btn btn-info btn-xs down_line">下线</button>' +
                                 '{{}}}'+
                                 '{{if($data[i].isCheck===0){}}'+
                                  ' <button class="btn btn-default btn-xs glyphicon glyphicon-earphone isCheck  nochecked"></button>' +
                                '{{}else{}}'+
                                  ' <button class="btn btn-default btn-xs glyphicon glyphicon-earphone isCheck  ischecked"></button>' +
                                 '{{}}}'+
                                 '{{if($data[i].isValidity===0){}}'+
                                  ' <button class="btn btn-default btn-xs glyphicon glyphicon-ok isValidity nochecked"></button>' +
                                '{{}else{}}'+
                                  ' <button class="btn btn-default btn-xs glyphicon glyphicon-ok isValidity ischecked"></button>' +
                                 '{{}}}'+
                            '</td>' +
                        '</tr>' +
                        '<tr class="hidden_">' +
                            '<td colspan="8">' +
                                '<ul>'+
                                    '{{if($data[i].scope){}}'+
                                      '{{var $arr=$data[i].scope; for(var a=0;a<$arr.length;a++){}}' +
                                      '<li class="class2_item" data-id="{{=$arr[a].cat_id}}">{{=$arr[a].cat_name}}</li>'+
                                      '{{} }}' +
                                     '{{ } }}'+
                               '</ul>'+
                            '</td>' +
                        '</tr>' +
                        '{{} }}' +
                    '</tbody>')(data));
            }else{
                $("#shangpin_table").empty().append(tpl.compile('' +
                    '<thead><tr><th>编号</th><th>商家名称</th><th>商家电话</th><th>商家地址</th><th>商家坐标</th><th>经营范围</th><th>图片</th><th>操作</th></tr></thead>' +
                    '<tbody>' +
                    '<tr>' +
                    '<td colspan="8" class="text-center">无数据</td>'+
                    '</tr>'+
                    '</tbody>')());
            }
        },
        excel_table_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>编号</th><th>商品名称</th><th>一级分类 </th > <th> 二级分类 </th><th>三级分类</th><th> 商品品牌</th><th>销售价格</th><th>商品规格</th><th>商品单位</th><th>商品尺寸</th><th>商品颜色</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="bian_hao">' + o.id + '</td>' +
                        '<td style="vertical-align: middle" class="shangpin_name">' + o.names + '</td>' +
                        '<td style="vertical-align: middle" class="class_one"><b>' + o.types + '</b></td>' +
                        '<td style="vertical-align: middle" class="class_two"><b>' + o.class + '</b></td>' +
                        '<td style="vertical-align: middle" class="class_three">' + o.catName+ '</td>' +
                        '<td style="vertical-align: middle" class="shangpin_pinpai">' + o.brand + '</td>' +
                        '<td style="vertical-align: middle" class="shangpin_price"><span>' + o.price + '</span></td>' +
                        '<td style="mso-number-format:\'\\@\';vertical-align: middle" class="shangping_guige">' + o.specs + '</td>' +
                        '<td style="vertical-align: middle" class="shangping_danwei">' + o.unit + '</td>' +
                        '<td style="mso-number-format:\'\\@\';vertical-align: middle" class="shangping_chicun">' + o.size + '</td>' +
                        '<td style="vertical-align: middle" class="shangping_color">' + o.color + '</td>' +
                        '</tr>';
                });
            } else {
                string_body = "<tr class='text-center'><td colspan='14'>没有数据</td></tr>";
            }

            $("#shangpin_table").empty().append(string_head + string_body);
        },
        huoqu_url_set: function (file) {
            var form_reader = new FileReader();
            form_reader.onload = function (e) {
                $(".set_img_wrap").append("<img src='" + e.target.result + "'/>");
            };
            form_reader.readAsDataURL(file);
        },
        huoqu_url_add: function (file) {
            var form_reader = new FileReader();
            form_reader.onload = function (e) {
                $(".add_img_wrap").append("<img src='" + e.target.result + "'/>");
            };
            form_reader.readAsDataURL(file);
        },
        tianjia: function () {
                $(".loading").fadeIn(200);
                var form_data = new FormData();
                form_data.append("banner", $("#add_input_file")[0].files[0]);
                var scopeId=[];
                $.each($("#add_yewu .active_yewu"),function () {
                    scopeId.push($(this).attr("data-id"));
                });
                console.log(scopeId);
                form_data.append("scopeId",scopeId);
                form_data.append("name", $.trim($("#add_name").val()));
                form_data.append("phone", $.trim($("#add_phone").val()));
                form_data.append("addr",$.trim($("#select_address").text()) );
                form_data.append("longitude",$.trim($("#select_point_lng").text()) );
                form_data.append("latitude", $.trim($("#select_point_lat").text()));
                form_data.append("token", get_cookie("key"));
                $.ajax({
                    url:JdIp + "admin/shops/add",
                    data: form_data,
                    type: "post",
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        console.log(res);
                        $(".loading").fadeOut(200);
                        if (res.resStatus === 1) {
                            $("#add_shangjia input").val("");
                            $(".add_img_wrap").empty();
                            houtai.shop_list_ajax(houtai.list_ajax_page);
                            $("#add_yewu .item_select").removeClass("active_yewu");
                            houtai.hint("<p class='text-success'>添加成功了！</p>");
                            add_map.remove([isClickMarker]);
                            $("#select_point_lng").text("");
                            $("#select_point_lat").text("");
                            $("#select_address").text("未选择");
                            isClickMarker=null;
                            $("#add_shangjia").fadeOut();
                        }
                    },
                    error: function (res) {
                        $(".loading").fadeOut(200);
                        houtai.hint("<p>添加失败了！</p>");
                    },
                    processData: false,  // 告诉jQuery不要去处理发送的数据
                    contentType: false   // 告诉jQuery不要去设置Content-Type请求头
                });
        },
        xiugai: function () {
            $(".loading").fadeIn(200);
            var form_data = new FormData();
            form_data.append("id", houtai.linshi_id);
            form_data.append("banner", $("#set_input_file")[0].files[0]);
            var scopeId=[];
            $.each($("#set_yewu .active_yewu"),function () {
                scopeId.push($(this).attr("data-id"));
            });
            form_data.append("scopeId",scopeId);
            form_data.append("name", $.trim($("#set_name").val()));
            form_data.append("phone", $.trim($("#set_phone").val()));
            form_data.append("addr",$.trim($("#set_select_address").text()) );
            form_data.append("longitude",$.trim($("#set_select_point_lng").text()) );
            form_data.append("latitude", $.trim($("#set_select_point_lat").text()));
            form_data.append("token", get_cookie("key"));
            $.ajax({
                url:JdIp + "admin/shops/edit",
                data: form_data,
                type: "post",
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    $(".loading").fadeOut(200);
                    if (res.resStatus === 1) {
                        $("#set_shangjia input").val("");
                        $(".set_img_wrap").empty();
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        houtai.hint("<p class='text-success'>修改成功了！</p>");
                        $("#set_shangjia").fadeOut();
                    }
                },
                error: function (res) {
                    $(".loading").fadeOut(200);
                    houtai.hint("<p>修改失败了！</p>");
                },
                processData: false,  // 告诉jQuery不要去处理发送的数据
                contentType: false   // 告诉jQuery不要去设置Content-Type请求头
            });
        },
        remove_img: function () {
            $.ajax({
                url: ip + 'index.php?s=admincms/product/opto',
                type: "post",
                data: {
                    tp: "delimg",
                    id: houtai.linshi_id,
                    imgid: houtai.linshi_img_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#modal_wrap").fadeOut();
                        houtai.hint("<span style='color:#0f0;'>删除成功了?</span>");
                        if (res.data.imgall[0].img === 0) {
                            $("#other_img").empty();
                            for (var i = 0; i < $("table tr>.bian_hao").length; i++) {
                                if ($("table tr>.bian_hao").eq(i).text() === houtai.linshi_id) {
                                    $("table tr>.bian_hao").eq(i)
                                        .parents("tr").find(".shangpin_imgs").empty();
                                    break;
                                }
                            }
                        } else {
                            //其他图片写入修改
                            var imgsstring = "";
                            $.each(res.data.imgall, function (i, o) {
                                imgsstring += '<div><img src="' + o.img + '"><b>' + o.imgid + '</b><span class="glyphicon glyphicon-trash remove_img"></span></div>';
                            });
                            $("#other_img").empty().append(imgsstring);
                            var imgs_string = '';
                            $.each(res.data.imgall, function (i, o) {
                                imgs_string += '<p>' + o.img + '</p><b>' + o.imgid + '</b>';
                            });
                            for (var i1 = 0; i1 < $("table tr>.bian_hao").length; i1++) {
                                if ($("table tr>.bian_hao").eq(i1).text() === houtai.linshi_id) {

                                    $("table tr>.bian_hao").eq(i1)
                                        .parents("tr").find(".shangpin_imgs").empty().append(imgs_string);
                                    break;
                                }
                            }
                        }

                    }else{
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("图片删除失败了！！");
                }
            });
        },
        down_line: function () {
            $.ajax({
                url: JdIp + "admin/shops/upAndDown",
                type: "post",
                data: {
                    status: 0,
                    id: [houtai.linshi_id],
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                       houtai.hint("<p class='text-success'>下线成功了！</p>");
                       houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("下架失败了！！");
                }
            });
        },
        up_line: function () {
            $.ajax({
                url:JdIp + "admin/shops/upAndDown",
                type: "post",
                data: {
                    status: 1,
                    id: [houtai.linshi_id],
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        houtai.hint("<p class='text-success'>下线成功了！</p>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("上线失败了");
                }
            });
        },
        checked:function () {
            $.ajax({
                url:JdIp + "admin/shops/isCheck",
                type: "post",
                data: {
                    shop_id:houtai.linshi_id,
                    isCheck:1,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        houtai.hint("<p class='text-success'>修改为已核实！</p>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("修改失败了");
                }
            });
        },
        nochecked:function () {
            $.ajax({
                url:JdIp + "admin/shops/isCheck",
                type: "post",
                data: {
                    shop_id:houtai.linshi_id,
                    isCheck:0,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        houtai.hint("<p class='text-success'>修改为未核实！</p>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("修改失败了");
                }
            });
        },
        isvalid:function () {
            $.ajax({
                url:JdIp + "admin/shops/isValidity",
                type: "post",
                data: {
                    shop_id:houtai.linshi_id,
                    isValidity:1,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        houtai.hint("<p class='text-success'>修改为有效！</p>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("修改失败了");
                }
            });
        },
        novalid:function () {
            $.ajax({
                url:JdIp + "admin/shops/isValidity",
                type: "post",
                data: {
                    shop_id:houtai.linshi_id,
                    isValidity:0,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 1) {
                        houtai.hint("<p class='text-success'>修改为无效！</p>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("修改失败了");
                }
            });
        },
        remove: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/opto",
                type: "post",
                data: {
                    tp: "del",
                    id: houtai.linshi_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#modal_wrap").fadeOut(200);
                        for (var i = 0; i < $("table tr .bian_hao").length; i++) {
                            if ($("table tr .bian_hao").eq(i).text() == houtai.linshi_id) {
                                var name = $("table tr .bian_hao").eq(i)
                                    .parents("tr").find(".shangpin_name").text();
                                houtai.hint("<p style='color:#0f0;'>删除" + name + "成功了</p>");
                                $("table tr .bian_hao").eq(i)
                                    .parents("tr")
                                    .remove();
                                break;
                            }
                        }

                    } else if (res.resStatus === 4) {
                        houtai.hint("本商品已在限时抢购中不能删除！！");
                    } else {
                        houtai.hint(res.message);
                    }

                },
                error: function () {
                    houtai.hint("操作失败了");
                }
            });
        },
        all_up: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/opto",
                type: "post",
                data: {
                    tp: "show",
                    id: houtai.linshi_arrid,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        for (var i = 0, name = ""; i < houtai.linshi_arrid.length; i++) {
                            for (var a = 0; a < $("table tr .bian_hao").length; a++) {
                                if ($("table tr .bian_hao").eq(a).text() == houtai.linshi_arrid[i]) {
                                    $("table tr .bian_hao").eq(a)
                                        .parents("tr")
                                        .find(".glyphicon-open")
                                        .removeClass("glyphicon-open btn-success up_line")
                                        .addClass("glyphicon-saved btn-info down_line")
                                        .text("下架");

                                    name += $("table tr .bian_hao").eq(a)
                                            .parents("tr").find(".shangpin_name").text() + (i == houtai.linshi_arrid.length - 1 ? "" : "、");
                                }
                            }

                        }
                        $("#modal_wrap").fadeOut(200);
                        houtai.hint("<p style='color:#0f0;'>上架" + name + "成功了</p>");
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("操作失败了");
                }
            });
        },
        all_down: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/opto",
                type: "post",
                data: {
                    tp: "hide",
                    id: houtai.linshi_arrid,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        for (var i = 0, name = ""; i < houtai.linshi_arrid.length; i++) {
                            for (var a = 0; a < $("table tr .bian_hao").length; a++) {
                                if ($("table tr .bian_hao").eq(a).text() == houtai.linshi_arrid[i]) {
                                    $("table tr .bian_hao").eq(a)
                                        .parents("tr")
                                        .find(".glyphicon-saved")
                                        .removeClass("glyphicon-saved btn-info down_line")
                                        .addClass("glyphicon-open btn-success up_line")
                                        .text("上架");

                                    name += $("table tr .bian_hao").eq(a)
                                            .parents("tr").find(".shangpin_name").text() + (i == houtai.linshi_arrid.length - 1 ? "" : "、");
                                }
                            }
                        }
                        $("#modal_wrap").fadeOut(200);
                        houtai.hint("<p style='color:#0f0;'>下架" + name + "成功了</p>");
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("操作失败了");
                }
            });
        },
        up_hot: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/marks",
                type: "post",
                data: {
                    id: houtai.linshi_id,
                    mark: 1,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        for (var i = 0; i < $("table tr .bian_hao").length; i++) {
                            if ($("table tr .bian_hao").eq(i).text() == houtai.linshi_id) {
                                $("table tr .bian_hao").eq(i)
                                    .parents("tr")
                                    .find(".up_hot")
                                    .removeClass("up_hot")
                                    .addClass("down_hot");
                                $("#modal_wrap").fadeOut(200);
                                var name = $("table tr .bian_hao").eq(i)
                                    .parents("tr").find(".shangpin_name").text();
                                houtai.hint("<p style='color:#0f0;'>" + name + "已改为<span style='color:#f10;'>热卖</span></p>");
                                break;
                            }
                        }

                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("操作失败了");
                }
            });
        },
        down_hot: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/marks",
                type: "post",
                data: {
                    id: houtai.linshi_id,
                    mark: 0,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        for (var i = 0; i < $("table tr .bian_hao").length; i++) {
                            if ($("table tr .bian_hao").eq(i).text() == houtai.linshi_id) {
                                $("table tr .bian_hao").eq(i)
                                    .parents("tr")
                                    .find(".down_hot")
                                    .removeClass("down_hot")
                                    .addClass("up_hot");
                                $("#modal_wrap").fadeOut(200);
                                var name = $("table tr .bian_hao").eq(i)
                                    .parents("tr").find(".shangpin_name").text();
                                houtai.hint("<p style='color:#0f0;'>" + name + "已改为<span style='color:#f10;'>非热卖</span></p>");
                                break;
                            }
                        }

                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("操作失败了");
                }
            });
        },
        record_ajax: function (page_number) {
            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + "index.php?s=admincms/logproduct/showLog",
                type: "post",
                data: {
                     id:houtai.linshi_id,
                    token: get_cookie("key"),
                    page:n,
                    types:houtai.linshi_search.type,
                    time_start: houtai.linshi_search.time_start,
                    time_end: houtai.linshi_search.time_end

                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.record_string(res);
                    $("#page1").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totapage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.record_ajax(pageIndex);
                        }
                    });

                }
            });
        },
        record_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>操作人</th><th>操作类型</th><th>操作时间</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var string1 = "";
                    var string2="";
                    var btn1="";
                    var btn2="";
                    if (o.type_n ==="alter"||o.type_n==="add") {
                        btn1="<span style='margin-left:10px;cursor:pointer;' class='glyphicon glyphicon-chevron-down'></span>";
                        btn2='class="btn1"';
                        var supplier_sring="";
                        $.each(o.obj.supplier,function (i,o) {
                            supplier_sring+= '<p><span>供应商 '+(i+1)+'：</span><span>' + o.sitenames + '</span></p>';
                        });
                        string1 = '<tr style="display: none">' +
                            '<td colspan="3" style="vertical-align: middle;background-color: rgb(251,251,244);text-align:start;" class="record_message">' +
                            '<p><span>产品名称：</span><span>' + o.obj.names + '</span></p>' +
                            '<p><span>别名：</span><span>' + o.obj.other_name + '</span></p>' +
                            '<p><span>销售价格：</span><span>' + o.obj.price + '</span></p>' +
                            '<p><span>一级分类：</span><span>' + o.obj.tid + '</span></p>' +
                            '<p><span>二级分类：</span><span>' + o.obj.cid + '</span></p>' +
                            '<p><span>品牌：</span><span>' + o.obj.brand + '</span></p>' +
                            '<p><span>规格：</span><span>' + o.obj.specs + '</span></p>' +
                            '<p><span>尺寸：</span><span>' + o.obj.size + '</span></p>' +
                            '<p><span>颜色：</span><span>' + o.obj.color + '</span></p>' +
                            '<p><span>单位：</span><span>' + o.obj.unit + '</span></p>' +
                            supplier_sring+
                            '</td>' +
                            '</tr>';
                    }else if(o.type_n === "time"){
                        btn1="<span style='margin-left:10px;cursor:pointer;' class='glyphicon glyphicon-chevron-down'></span>";
                        btn2='class="btn1"';
                        if(o.obj.type_n==="add"||o.obj.type_n==="alter"){
                            string2='<div>' +
                                '<p><span>价格：</span><span>'+o.obj.obj.price+'</span></p>' +
                                '<p><span>时间：</span><span>'+o.obj.obj.time+'</span></p>' +
                                '<p><span>序号：</span><span>'+o.obj.obj.index+'</span></p>' +
                                '</div>';
                        }

                        string1 = '<tr style="display:none">' +
                            '<td colspan="3" style="vertical-align: middle;background-color: rgb(251,251,244);text-align:start;" class="record_message">' +
                            '<p style="margin-bottom: 8px;"><span>类型：</span><span>' + o.obj.type+ '</span></p>' +string2+
                            '</td>' +
                            '</tr>';
                    }
                    string_body += '<tr '+btn2+'>' +
                        '<td style="vertical-align: middle" class="record_name">' + o.name + '</td>' +
                        '<td style="vertical-align: middle" class="record_type">' + o.type +btn1+ '</td>' +
                        '<td style="vertical-align: middle" class="record_time">' + o.time + '</td>' +
                        '</tr>';
                    string_body += string1;
                });
            } else {
                string_body = "<tr class='text-center'><td colspan='14'>没有数据</td></tr>";
            }

            $("#supervise_record table").empty().append(string_head + string_body);
        },
        control_type_ajax:function () {
            $.ajax({
                url:ip+"index.php?s=admincms/logproduct/logListQuery",
                type:"post",
                data:{
                  token:get_cookie("key")
                },
                success:function (_res) {
                    var res=JSON.parse(_res);
                    if(res.resStatus===0){
                        var string1='<option value="">全部</option>';
                        $.each(res.data,function (i,o) {
                            string1+='<option value="'+o.types+'">'+o.names+'</option>';
                        });
                        $("#taocan_select1").empty().append(string1);
                    }

                },
                error:function () {
                   houtai.hint("操作类型请求失败！！");
                }
            });
        },
        gongyingshang_ajax:function () {
            $.ajax({
                url: ip + "index.php?s=admincms/supplier/show",
                type: "post",
                data:{
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res);
                    if(res.resStatus===0){
                        var set_string='<option value="">请选择</option>';
                        var seller_string='<option value="">全部</option>';
                        $.each(res.data,function (i,o) {
                            set_string+='<option value="'+o.id+'">'+o.sitenames+'</option>';
                            seller_string+='<option value="'+o.id+'">'+o.sitenames+'</option>';

                        });
                        $("#shangpin_gongyingshang").empty().append(set_string);
                        $("#seller").empty().append(seller_string);
                    }else{
                        houtai.hint("供应商请求失败！！");
                    }
                },
                error:function () {
                    houtai.hint("供应商请求失败！！");
                }

            });
        }
    };
    // 初始类
    houtai.chushi_class();
    // 搜索时临时存放
    houtai.linshi_search.class1 = $("#select1").val();
    // 请求数据
    houtai.shop_list_ajax();
    //时间插件
    $('#input_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });





    // 添加商家按钮
    $("#add_btn").bind("click",function () {
        $("#add_shangjia").fadeIn();
    });
    // 选择状态
    $("#select_status").bind("change",function () {
       houtai.linshi_search.state=$(this).val()===""?null: $(this).val();
       houtai.shop_list_ajax();
    });
    // 核实选择
    $("#select_isverify").bind("change",function () {
       houtai.linshi_search.isCheck =$(this).val()===""?null: $(this).val();
       houtai.shop_list_ajax();
    });
    // 有效选择
    $("#select_isvalid").bind("change",function () {
       houtai.linshi_search.isValidity =$(this).val()===""?null: $(this).val();
       houtai.shop_list_ajax();
    });

    // 搜索商家
    $("#search_btn").bind("click",function () {
        if($.trim($("#search_input").val())===""){
            houtai.hint("请输入内容后搜索");
            $("#search_input").focus();
        }else{
            houtai.linshi_search.search=$.trim($("#search_input").val());
            houtai.linshi_search.state=null;
            houtai.shop_list_ajax();
        }
    });
    $("#form_search").bind("submit",function (e) {
        if($.trim($("#search_input").val())===""){
            houtai.hint("请输入内容后搜索");
            $("#search_input").focus();
        }else{
            houtai.linshi_search.search=$.trim($("#search_input").val());
            houtai.linshi_search.state=null;
            houtai.shop_list_ajax();
        }
        e.preventDefault();
        return false;
    });

    // 修改商家按钮
    $("#set").bind("click", function () {
        if(houtai.jiancha()){
            if($("#set_yewu .active_yewu").length===0){
                houtai.hint("请选择主营业务！");
            }else if($("#set_select_point_lat").text()===""){
                houtai.hint("请选择商家地址（在地图上点击位置）！");
            }else if(houtai.click_yes()) {
                houtai.xiugai();
            }
        }
    });
    // 添加按钮
    $("#add").bind("click", function () {
        if(houtai.jiancha()){
            if($("#add_yewu .active_yewu").length===0){
                houtai.hint("请选择主营业务！");
            }else if($("#select_point_lat").text()===""){
                houtai.hint("请选择商家地址（在地图上点击位置）！");
            }else if (houtai.click_yes()) {
                houtai.tianjia();
            }
        }
    });
    // 修改
    $("table").on("click", ".set", function () {
        $(window).scrollTop(0);
        houtai.linshi_id = $(this).parents("tr").find(".shangjia_id").text();
        $("#set_name").val($(this).parents("tr").find(".shangjia_name").text());
        $("#set_phone").val($(this).parents("tr").find(".shangjia_phone").text());
        $("#set_yewu .item_select").removeClass("active_yewu");
        $(this).parents("tr").next().find(".class2_item").each(function () {
            console.log(this);
            var id=$(this).attr('data-id');
            $("#set_yewu .item_select[data-id="+id+"]").addClass("active_yewu");
        });
        $("#set_img").attr("src",$(this).parents("tr").find(".shangjia_banner img").attr("src"));
        $("#set_select_address").text($(this).parents("tr").find(".shangjia_address").text());
        $("#set_select_point_lng").text($(this).parents("tr").find(".lng").text());
        $("#set_select_point_lat").text($(this).parents("tr").find(".lat").text());
        // 判断是否存在点
        if(setisClickMarker){
            setisClickMarker.setPosition( [$(this).parents("tr").find(".lng").text(),$(this).parents("tr").find(".lat").text()]);
        }else{
            setisClickMarker=  new AMap.Marker({
                position: [$(this).parents("tr").find(".lng").text(),$(this).parents("tr").find(".lat").text()],
                map: set_map,
                animation:'AMAP_ANIMATION_BOUNCE'
            });
        }
        set_map.setZoomAndCenter(18, [$(this).parents("tr").find(".lng").text(),$(this).parents("tr").find(".lat").text()]);
        $("#set_shangjia").fadeIn(200);
    });
    // 上线按钮
    $("table").on("click", ".up_line", function () {
        houtai.linshi_id = $(this).parents("tr").find(".shangjia_id").text();
        houtai.queren_type = 1;
        $("#modal_wrap").find(".modal-body").empty().append("你确定要上架<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>吗？");
        $("#modal_wrap").show();
    });
    // 下线按钮
    $("table").on("click", ".down_line", function () {
        houtai.linshi_id = $(this).parents("tr").find(".shangjia_id").text();
        houtai.queren_type = 2;
        $("#modal_wrap").find(".modal-body").empty().append("你确定要下架<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>吗？");
        $("#modal_wrap").show();

    });
    // 核实切换按钮
    $("table").on("click", ".isCheck", function () {
        houtai.linshi_id = $(this).parents("tr").find(".shangjia_id").text();
        if($(this).hasClass("nochecked")){
            $("#modal_wrap").find(".modal-body").empty().append("你确定要把<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>改为<span style='color: #1c1;'>已核实</span>吗？");
            $("#modal_wrap").show();
            houtai.queren_type = 3;
        }else{
            $("#modal_wrap").find(".modal-body").empty().append("你确定要把<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>改为<span style='color: #1c1;'>未核实</span>吗？");
            $("#modal_wrap").show();
            houtai.queren_type = 4;
        }

    });
    // 有效切换按钮
    $("table").on("click", ".isValidity", function () {
        houtai.linshi_id = $(this).parents("tr").find(".shangjia_id").text();
        if($(this).hasClass("nochecked")){
            $("#modal_wrap").find(".modal-body").empty().append("你确定要把<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>改为 <span style='color: #1c1;'>有效</span>吗？");
            $("#modal_wrap").show();
            houtai.queren_type = 5;
        }else{
            $("#modal_wrap").find(".modal-body").empty().append("你确定要把<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>改为<span style='color: #1c1;'>无效</span>吗？");
            $("#modal_wrap").show();
            houtai.queren_type = 6;
        }
    });

    //选择分类
    $(".yewu").on("click",".item_yewu .item_select",function () {
        $(this).toggleClass("active_yewu");
    });
    //选择全部分类
    $(".yewu").on("click",".item_yewu .all_select",function () {
        $(this).toggleClass("active_yewu_all");
        if($(this).hasClass('active_yewu_all')){
            $(this).siblings(".item_select").addClass("active_yewu");
        }else{
            $(this).siblings(".item_select").removeClass("active_yewu");
        }

    });
    // 确认
    $("#confirm").bind("click", function () {
        switch (houtai.queren_type){
            case 1:houtai.up_line();
            break;
            case 2:houtai.down_line();
            break;
            case 3:houtai.checked();
            break;
            case 4:houtai.nochecked();
            break;
            case 5:houtai.isvalid();
            break;
            case 6:houtai.novalid();
            break;
        }

    });





    $("#input_time_btn").bind("click", function () {
        $("#input_time").click();
    });
    $("#excel_btn").bind("click",function () {});
    console.log($("#excel_btn").data("events"));
    // 一级下拉事件
    $("#select1").bind("change", function () {
        sessionStorage.one=$(this).val();
        for (var b = 0,string2="<option value=''>全部</option>",string3="<option value=''>全部</option>"; b < houtai.arr.length; b++) { //循环分类查找对应的子集
            if (houtai.arr[b].pid == sessionStorage.one) {
                sessionStorage.two = houtai.arr[b].activity_list[0].cid; //记录这次操作
                sessionStorage.three = houtai.arr[b].activity_list[0].cats_list[0]&&houtai.arr[b].activity_list[0].cats_list[0].catId; //记录这次操作
                for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                    string2 += '<option value="' + houtai.arr[b].activity_list[a].cid + '">' + houtai.arr[b].activity_list[a].classnames + '</option>';
                }
                break;
            }
        }
        $("#select2").empty().append(string2);
        $("#select3").empty().append('<option value="">全部</option>');
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 =$("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state =$("#select4").val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 二级下拉事件
    $("#select2").bind("change", function () {
        if($(this).val()===""){
            $("#select3").empty().append('<option value="">全部</option>');
        }else{
            for (var b = 0,string3="<option value=''>全部</option>"; b < houtai.arr.length; b++) { //循环分类查找对应的子集
                if (houtai.arr[b].pid == sessionStorage.one) {
                    for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                        if (houtai.arr[b].activity_list[a].cid == $(this).val()) {
                            for (var bb = 0; bb < houtai.arr[b].activity_list[a].cats_list.length; bb++) {
                                string3 += '<option value="' + houtai.arr[b].activity_list[a].cats_list[bb].catId + '">' + houtai.arr[b].activity_list[a].cats_list[bb].catName + '</option>';
                            }
                            $("#select3").empty().append(string3);
                            break;
                        }
                    }
                    break;
                }
            }
        }

        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 =$("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state = $("#select4").val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 三级下拉事件
    $("#select3").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state = $("#select4").val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 状态下拉事件
    $("#select4").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state = $(this).val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 搜索本类
    $("#search_ben").bind("click", function () {
        if ($("#search_input").val() === "") {
            houtai.hint("请输入再搜索");
            $("#search_input").focus();
        } else {
            houtai.linshi_search.class1 = $("#select1").val();
            houtai.linshi_search.class2 = $("#select2").val();
            houtai.linshi_search.state = $("#select4").val();
            houtai.linshi_search.search = $.trim($("#search_input").val());
            houtai.shop_list_ajax();
        }

    });
    // 搜索全站
    $("#search_all").bind("click", function () {
        if ($("#search_input").val() === "") {
            houtai.hint("请输入再搜索");
            $("#search_input").focus();
        } else {
            houtai.linshi_search.class1 = "";
            houtai.linshi_search.class2 = "";
            houtai.linshi_search.state = "";
            houtai.linshi_search.search = $.trim($("#search_input").val());
            houtai.shop_list_ajax();
        }
    });
    //按供应商搜索
    $("#seller").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state =  $("#select4").val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    //自营所搜
    $("#isAutotrophy").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.class3 = $("#select3").val();
        houtai.linshi_search.state =  $("#select4").val();
        houtai.linshi_search.supplier=$("#seller").val();
        houtai.linshi_search.types=$("#isAutotrophy").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 修改一级下拉事件
    $("#set-select1").bind("change", function () {
        for (var b = 0,string2="",string3=""; b < houtai.arr.length; b++) { //循环分类查找对应的子集
            if (houtai.arr[b].pid == $(this).val()) {
                for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                    string2 += '<option value="' + houtai.arr[b].activity_list[a].cid + '">' + houtai.arr[b].activity_list[a].classnames + '</option>';
                    if(houtai.arr[b].activity_list[a].cid == houtai.arr[b].activity_list[0].cid ){
                        for(var bb=0;bb<houtai.arr[b].activity_list[a].cats_list.length;bb++){
                            string3 += '<option value="' +houtai.arr[b].activity_list[a].cats_list[bb].catId+ '">' + houtai.arr[b].activity_list[a].cats_list[bb].catName+ '</option>';
                        }
                        $("#set-select3").empty().append(string3);
                    }
                }
                break;
            }
        }
        $("#set-select2").empty().append(string2);
        houtai.pinpai_fuzhi($("#set-select3").val());
    });
    // 修改二级下拉事件
    $("#set-select2").bind("change", function () {
        for (var b = 0,string3=""; b < houtai.arr.length; b++) { //循环分类查找对应的子集
            if (houtai.arr[b].pid == $("#set-select1").val() ) {
                for (var a = 0; a < houtai.arr[b].activity_list.length; a++) {
                    if (houtai.arr[b].activity_list[a].cid == $(this).val()) {
                        for (var bb = 0; bb < houtai.arr[b].activity_list[a].cats_list.length; bb++) {
                            string3 += '<option value="' + houtai.arr[b].activity_list[a].cats_list[bb].catId + '">' + houtai.arr[b].activity_list[a].cats_list[bb].catName + '</option>';
                        }
                        $("#set-select3").empty().append(string3);
                        break;
                    }
                }
                break;
            }
        }
        houtai.pinpai_fuzhi($("#set-select3").val());
    });
    // 修改三级下拉事件
    $("#set-select3").bind("change", function () {
        houtai.pinpai_fuzhi($(this).val());
    });
    //品牌下拉
    $("#down_menu").on("click", "li", function () {
        $("#set_brand").val($(this).find("a").text());
    });
    // 规格下拉
    $("#down_menu_guige").on("click", "li", function () {
        $("#set_guige").val($(this).find("a").text());
    });
    // 单位下拉
    $("#down_menu_danwei").on("click", "li", function () {
        $("#set_danwei").val($(this).text());
    });
    // 单位删除
    $("#down_menu_danwei").on("click", "b", function (e) {
        e.stopPropagation();
        var arr = getarr(localStorage.arr_danwei);
        arr.splice($(this).parents("li").index(), 1);
        setarr(arr);
        $(this).parents("li").remove();
    });
    // 尺寸下拉
    $("#down_menu_chicun").on("click", "li", function () {
        $("#set_chicun").val($(this).text());
    });
    // 尺寸删除
    $("#down_menu_chicun").on("click", "b", function (e) {
        e.stopPropagation();
        var arr_chicun = getarr(localStorage.arr_chicun);
        arr_chicun.splice($(this).parents("li").index(), 1);
        setchicun_arr(arr_chicun);
        $(this).parents("li").remove();
    });
    // 颜色下拉
    $("#down_menu_color").on("click", "li", function () {
        $("#set_color").val($(this).find("a").text());
    });

    // 全选事件
    $("#all_select>input").bind("click", function (e) {
        if ($(this).prop("checked")) {
            $(this).prop("checked", true);
            $("table>tbody>tr>td>input:not(':disabled')").prop("checked", true);
        } else {
            $(this).prop("checked", false);
            $("table>tbody>tr>td>input:not(':disabled')").prop("checked", false);
        }
    });
    // 全选事件
    $("#all_select>span").bind("click", function (e) {
        if ($(this).next().prop("checked")) {
            $(this).next().prop("checked", false);
            $("table>tbody>tr>td>input:not(':disabled')").prop("checked", false);
        } else {
            $(this).next().prop("checked", true);
            $("table>tbody>tr>td>input:not(':disabled')").prop("checked", true);
        }
    });
    // 单选事件
    $("table").on("click", "input", function (e) {
        if ($("table>tbody>tr>td>input:checked").length !== $("table>tbody>tr>td>input").length) {
            $("#all_select>input").prop("checked", false);
        } else {
            $("#all_select>input").prop("checked", true);
        }
    });
    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });
    // 默认图片上传按钮
    $("#add_up_button").bind("click", function () {
        $("#add_input_file").click();
    });
    // 默认图片选择时事件
    $("#add_input_file").change(function () {
        $(".add_img_wrap").empty();
        houtai.huoqu_url_add($(this)[0].files[0]);
    });
    // 其他图片上传按钮
    $("#set_up_button").bind("click", function () {
        $("#set_input_file").click();
    });
    // 其他图片选择时事件
    $("#set_input_file").change(function () {
        $(".set_img_wrap").empty();
            houtai.huoqu_url_set($(this)[0].files[0]);
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 取消操作
    $(".cancle_control").bind("click", function () {
        $(this).parents(".set_shangpin").fadeOut(200);
        $(".add_img_wrap").empty();
        $(".set_img_wrap").empty();
        $("#add_input_file").val("");
        $("#set_input_file").val("");
    });
    //取消记录框按钮
    $("#close_record").bind("click", function () {
        $("#supervise_record").fadeOut();
    });
    //其他信息查看展开收缩按钮
    $("#shangpin_table").on("click",".click_class2>b",function () {
      $(this).parents("tr").next().fadeToggle();
    });
    //供应商下拉
    $("#shangpin_gongyingshang").bind("change",function () {
        if($(this).val()!==""){
            for(var i=0;i< $(".supplier_wrap .supplier_val").length;i++){
                if($(".supplier_wrap .supplier_val").eq(i).text()===$("#shangpin_gongyingshang").val()){
                    break;
                }
            }
            if(i===$(".supplier_wrap .supplier_val").length){
                $(".supplier_content").fadeIn();
                $(".supplier_wrap").append(
                    '<p>' +
                    '<b class="supplier_val">'+$(this).val()+'</b>' +
                    '<span class="supplier_index"> '+($(".supplier_wrap>p").length+1)+'</span>' +
                    '<span class="supplier_name">'+$("#shangpin_gongyingshang option:selected").text()+' </span>' +
                    '<b class="supplier_remove glyphicon glyphicon-trash"></b>' +
                    '</p>');
            }

        }

    });
    //删除临时供应商
    $(".supplier_content").on("click",".supplier_remove",function () {
        $(this).parents("p").remove();
        $.each($(".supplier_wrap>p .supplier_index"),function (i) {
            $(this).text(i+1);
        });
        $("#shangpin_gongyingshang").val("");
        if($(".supplier_wrap>p").length===0){
            $(".supplier_content").hide();

        }
    });
    // 删除
    $("table").on("click", ".remove", function () {
        houtai.linshi_id = $(this).parents("tr").find(".bian_hao").text();
        houtai.queren_type = 0;
        if ($(this).parents("tr").find(".glyphicon-open").length > 0) {
            $("#modal_wrap").find(".modal-body").empty().append("你确定要删除<span style='color:#f10;'>" + $(this).parents("tr").find('.shangpin_name').text() + "</span>吗？");
            $("#modal_wrap").show();
        } else {
            houtai.hint("<span style='font-size:30px;color:#2aabd2;'>" + $(this).parents("tr").find(".shangpin_name").text() + "</span>" + " 已上架,请点击下架后删除！");
        }
    });

    // 上热卖
    $("table").on("click", ".up_hot", function () {
        houtai.linshi_id = $(this).parents("tr").find(".bian_hao").text();
        houtai.queren_type = 6;
        $("#modal_wrap").find(".modal-body").empty().append("你确定要把<span style='color:#f10;'>" + $(this).parents("tr").find('.shangpin_name').text() + "</span>改为<span style='color:#faa;'>热卖</span>吗？");
        $("#modal_wrap").show();

    });
    // 下热卖
    $("table").on("click", ".down_hot", function () {
        houtai.linshi_id = $(this).parents("tr").find(".bian_hao").text();
        houtai.queren_type = 7;
        $("#modal_wrap").find(".modal-body").empty().append("你确定要把<span style='color:#f10;'>" + $(this).parents("tr").find('.shangpin_name').text() + "</span>改为<span style='color:#faa;'>非热卖</span>吗？");
        $("#modal_wrap").show();

    });
    //二维码
    $("table").on("click", ".erweima", function () {
        houtai.linshi_id=$(this).parents("tr").find(".bian_hao").text();
        var has_item=false;
        var _name=$(this).parents("tr").find(".shangpin_name").text();
       $.each($(".name_erweima>span"),function(){
           var a=$(this).text();
          if(a===_name) {
              has_item=true;
          }
       });
       if(has_item){
           houtai.hint("已经有这个商品!!");
       }else{
           var erweima_name="erweima"+Date.now();
           $("#erweima_wrap>div").append('' +
               '<div class="item_erweima">' +
               '<div class="img_erweima '+erweima_name+'"></div> ' +
               '<p class="brand_erweima">编号：<span>'+$(this).parents("tr").find(".bian_hao").text()+'</span></p> ' +
               '<p class="brand_erweima">品牌：<span>'+$(this).parents("tr").find(".shangpin_pinpai").text()+'</span></p> ' +
               '<p class="gui_erweima">规格：<span>'+$(this).parents("tr").find(".shangping_guige").text()+'</span></p> ' +
               '<p class="name_erweima"><span>'+$(this).parents("tr").find(".shangpin_name").text()+'</span></p> ' +
               '</div>'
           );

           new QRCode($("."+erweima_name)[0], {
               text: houtai.linshi_id,
               width: 300,
               height: 300
           });
           $("#erweima_wrap").show();
       }



    });
    //打印二维码
    $("#pring_erweima").bind("click",function () {
        $(".item_erweima").css({
            "width": 340,
            "height":476,
            "padding-top": 10,
            "padding-bottom": 20,
            "float": "left",
            "border":"1px solid #888"
        });
        $(".item_erweima>p").css({
            "text-align": "center",
            "margin-bottom": 10,
            "font-size":18
        });
        $(".img_erweima").css({
            "width": 300,
           "height": 300,
            "margin":"20px 20px 20px 20px"
        });

         $("#erweima_wrap>div").jqprint();
        $("#erweima_wrap").hide();
        $("#erweima_wrap>div").empty();
    });
    //关闭二维码
    $(".close_erweima").bind("click",function () {
        $("#erweima_wrap").hide();
        $("#erweima_wrap>div").empty();
    });
    //记录查询
    $("table").on("click", ".record", function () {
        $(window).scrollTop(0);
        houtai.linshi_id = $(this).parents("tr").find(".bian_hao").text();
        houtai.record_ajax();
        $("#supervise_record").fadeIn();
    });
    //按类型搜索
    $("#taocan_select1").bind("change",function () {
      houtai.linshi_search.type=$(this).val();
      houtai.linshi_search.time_start="";
      houtai.linshi_search.time_end="";
      houtai.record_ajax();
    });
    // 按时间搜索
    $("#search_time").bind("click", function () {
        if ($.trim($("#input_time").val()) === "") {
            $("#input_time").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                console.log($("#input_time").val());
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.linshi_search.type="";
                houtai.linshi_search.time_start = new Date($("#input_time").val().match(regexp)[1]).getTime() / 1000;
                houtai.linshi_search.time_end = new Date($("#input_time").val().match(regexp)[2]).getTime() / 1000;
                houtai.record_ajax();
            }
        }

    });
    //记录查询点击隐藏或展开事件
    $("#supervise_record").on("click",".btn1",function () {
     $(this).next().fadeToggle();
    });
    // 全选上架按钮
    $("#all_up").bind("click", function () {
        houtai.linshi_arrid = [];
        houtai.queren_type = 3;
        if ($("table>tbody>tr>td>input:checked").parents("tr").find(".up_line").length > 0) {
            for (var i = 0,
                     string = ""; i < $("table>tbody>tr>td>input:checked").parents("tr").find(".up_line").length; i++) {
                houtai.linshi_arrid.push($("table>tbody>tr>td>input:checked").parents("tr").find(".up_line").eq(i).parents("tr").find(".bian_hao").text());
                string += "<span style='color:#f10;'>" + (i === 0 ? '' : '、') + $("table>tbody>tr>td>input:checked").parents("tr").find(".up_line").eq(i).parents('tr').find('.shangpin_name').text() + "</span>";
            }
            $("#modal_wrap").find(".modal-body").empty().append("你确定要上架" + string + "吗？");
            $("#modal_wrap").show();
        } else {
            houtai.hint("<p>请选择您要操作的商品</p>");
        }

    });
    // 全选下架按钮
    $("#all_down").bind("click", function () {
        houtai.linshi_arrid = [];
        houtai.queren_type = 4;
        if ($("table>tbody>tr>td>input:checked").parents("tr").find(".down_line").length > 0) {
            for (var i = 0,
                     string = ""; i < $("table>tbody>tr>td>input:checked").parents("tr").find(".down_line").length; i++) {
                houtai.linshi_arrid.push($("table>tbody>tr>td>input:checked").parents("tr").find(".down_line").eq(i).parents("tr").find(".bian_hao").text());
                string += "<span style='color:#f10;'>" + (i === 0 ? '' : '、') + $("table>tbody>tr>td>input:checked").parents("tr").find(".down_line").eq(i).parents('tr').find('.shangpin_name').text() + "</span>";
            }
            $("#modal_wrap").find(".modal-body").empty().append("你确定要下架" + string + "吗？");
            $("#modal_wrap").show();
        } else {
            houtai.hint("<p>请选择您要操作的商品</p>");
        }

    });
    // 删除图片按钮
    $("#other_img").on("click", ".remove_img", function () {
        houtai.linshi_img_id = $(this).prev().text();
        houtai.queren_type = 5;
        $(".modal-body").empty().append("您确定要删除这张图片吗？");
        $("#modal_wrap").fadeIn();
    });
    // 主图放大按钮
    $("#main_img").on("click", "img", function () {
        var imgH = $(this)[0].naturalHeight;
        var imgW = $(this)[0].naturalWidth;
        var windW = $(window).width();
        var windH = $(window).height();
        $(".img_show>img").css({
            left: $(this).offset().left,
            top: $(this).offset().top - $(window).scrollTop(),
            width: $(this).width(),
            height: $(this).height()
        });
        $(".img_show>img").attr("src", $(this).attr("src"));
        $(".img_show").fadeIn();
        if ((((imgW / imgH) > (windW / windH)) && (imgW < windW)) || (((imgW / imgH) <= (windW / windH)) && (imgH < windH))) {
            $(".img_show>img").css({
                left: (windW - imgW) / 2,
                top: (windH - imgH) / 2,
                width: imgW,
                height: imgH
            });
        } else if (((imgW / imgH) > (windW / windH)) && (imgW >= windW)) {
            $(".img_show>img").css({
                width: windW,
                height: windW * (imgH / imgW),
                top: (windH - (windW * (imgH / imgW))) / 2,
                left: 0
            });
        } else if (((imgW / imgH) <= (windW / windH)) && (imgH >= windH)) {
            $(".img_show>img").css({
                width: windH * (imgW / imgH),
                height: windH,
                left: (windW - (windH * (imgW / imgH))) / 2,
                top: 0
            });
        }
    });
    // 其他图放大按钮
    $("#other_img").on("click", "img", function () {
        $(".img_show>img").css({
            left: $(this).offset().left,
            top: $(this).offset().top - $(window).scrollTop(),
            width: $(this).width(),
            height: $(this).height()
        });
        $(".img_show>img").attr("src", $(this).attr("src"));
        $(".img_show").fadeIn();
        var imgH = $(this)[0].naturalHeight;
        var imgW = $(this)[0].naturalWidth;
        var windW = $(window).width();
        var windH = $(window).height();
        if ((((imgW / imgH) > (windW / windH)) && (imgW < windW)) || (((imgW / imgH) <= (windW / windH)) && (imgH < windH))) {
            $(".img_show>img").css({
                left: (windW - imgW) / 2,
                top: (windH - imgH) / 2,
                width: imgW,
                height: imgH
            });
        } else if (((imgW / imgH) > (windW / windH)) && (imgW >= windW)) {
            $(".img_show>img").css({
                width: windW,
                height: windW * (imgH / imgW),
                top: (windH - (windW * (imgH / imgW))) / 2,
                left: 0
            });
        } else if (((imgW / imgH) <= (windW / windH)) && (imgH >= windH)) {
            $(".img_show>img").css({
                width: windH * (imgW / imgH),
                height: windH,
                left: (windW - (windH * (imgW / imgH))) / 2,
                top: 0
            });
        }


    });
    // 放大图点击消失
    $(".img_show").bind("click", function () {
        $(this).fadeOut();
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});

